/************************************************************************/
/* GUI                                                                  */
/************************************************************************/
#include "GUI/Panels/TowerMenuPanel.h"

namespace TD_GUI {

	/************************************************************************/
	/* Default definitions                                                  */
	/************************************************************************/
	/*const string TowerMenuPanel::m_towerMenuBackground = "sd:/apps/MarioTD/resources/towers/tower-menu.png";*/
	const Coord2D TowerMenuPanel::m_menuPanelPosition(0.79981*screenWidth, 0.09375*screenHeight);
	const Coord2D TowerMenuPanel::m_menuPanelSize(0.18622*screenWidth, 0.19375*screenHeight);
	const Coord2D TowerMenuPanel::m_towerButtonSize(0.03125*screenWidth, 0.03125*screenWidth);

	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	TowerMenuPanel::TowerMenuPanel() : BasePanel(m_menuPanelPosition, m_menuPanelSize) {

 
		m_pLakituTowerButton = new BaseButton(m_menuPanelPosition + Coord2D(0.01862*screenWidth, 0.04125*screenHeight), m_towerButtonSize,
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/lakituButton.png", Color::PINK));

		m_pShyguyTowerButton = new BaseButton(m_menuPanelPosition + Coord2D(0.05865*screenWidth, 0.04125*screenHeight), m_towerButtonSize,
											TD_Resources::ResourceManager::loadImage("GUI/Buttons/shyguyButton.png", Color::PINK));
		

		m_pMagikoopaTowerButton = new BaseButton(m_menuPanelPosition + Coord2D(0.09869*screenWidth, 0.04125*screenHeight), m_towerButtonSize,
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/magikoopaButton.png", Color::PINK));

		m_pFlowerTowerButton = new BaseButton(m_menuPanelPosition + Coord2D(0.13873*screenWidth, 0.04125*screenHeight), m_towerButtonSize,
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/flowerButton.png", Color::PINK));

		m_pSnifitTowerButton = new BaseButton(m_menuPanelPosition + Coord2D(0.01862*screenWidth, 0.09125*screenHeight), m_towerButtonSize,
			TD_Resources::ResourceManager::loadImage("GUI/Buttons/sniftTowerButton.png", Color::PINK));

		m_isShowingTowerInfo = false;
	}

	TowerMenuPanel::TowerMenuPanel(const TowerMenuPanel &rhs) {

	}

	TowerMenuPanel & TowerMenuPanel::operator =(const TowerMenuPanel & rhs) {
		return(*this);
	}
	
	TowerMenuPanel::~TowerMenuPanel() {
		Utils::safeDelete<BaseButton>(m_pShyguyTowerButton);
		Utils::safeDelete<BaseButton>(m_pLakituTowerButton);
		Utils::safeDelete<BaseButton>(m_pFlowerTowerButton);
		Utils::safeDelete<BaseButton>(m_pMagikoopaTowerButton);
		Utils::safeDelete<BaseButton>(m_pSnifitTowerButton);
	}

	/************************************************************************/
	/* Rendering                                                            */
	/************************************************************************/

	void TowerMenuPanel::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		BasePanel::render(g_destSurface, deltaTime);
		if(m_pShyguyTowerButton != NULL) {
			m_pShyguyTowerButton->render(g_destSurface, deltaTime);
		}
		if(m_pLakituTowerButton != NULL) {
			m_pLakituTowerButton->render(g_destSurface, deltaTime);
		}
		if(m_pMagikoopaTowerButton != NULL) {
			m_pMagikoopaTowerButton->render(g_destSurface, deltaTime);
		}
		if(m_pFlowerTowerButton != NULL) {
			m_pFlowerTowerButton->render(g_destSurface, deltaTime);
		}
		if(m_pSnifitTowerButton != NULL) {
			m_pSnifitTowerButton->render(g_destSurface, deltaTime);
		}
	}

	interaction_t TowerMenuPanel::processEvent(const SDL_Event &event) {
		interaction_t interaction;
		interaction.type = noInteraction;
		
		Coord2D mousePos;
		if(event.type == SDL_MOUSEBUTTONDOWN ) { //If the left mouse button was pressed 	
			mousePos.x = event.button.x;
			mousePos.y = event.button.y;
			
			if(m_pShyguyTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerSelected;
				interaction.towerType = shyguyTower_lvl1;
				interaction.value = TowerFactory::getBaseTowerReference(interaction.towerType)->getPrice();
			} else if(m_pLakituTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerSelected;
				interaction.towerType = lakituTower_lvl1;
				interaction.value = TowerFactory::getBaseTowerReference(interaction.towerType)->getPrice();
			} else if(m_pMagikoopaTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerSelected;
				interaction.towerType = magikoopaTower_lvl1;
				interaction.value = TowerFactory::getBaseTowerReference(interaction.towerType)->getPrice();
			} else if(m_pFlowerTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerSelected;
				interaction.towerType = flowerTower_lvl1;
				interaction.value = TowerFactory::getBaseTowerReference(interaction.towerType)->getPrice();
			} else if(m_pSnifitTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerSelected;
				interaction.towerType = snifitTower_lvl1;
				interaction.value = TowerFactory::getBaseTowerReference(interaction.towerType)->getPrice();
			} else {
				SDLCursor::hideCursor(false);
				interaction.type = towerHideInserting;
			}

		} else if(event.type == SDL_MOUSEMOTION) {
			mousePos.x = event.motion.x;
			mousePos.y = event.motion.y;

			if(m_pShyguyTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerShowStaticInfo;
				interaction.towerType = shyguyTower_lvl1;
				m_isShowingTowerInfo = true;
			} else if(m_pLakituTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerShowStaticInfo;
				interaction.towerType = lakituTower_lvl1;
				m_isShowingTowerInfo = true;
			} else if(m_pMagikoopaTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerShowStaticInfo;
				interaction.towerType = magikoopaTower_lvl1;
				m_isShowingTowerInfo = true;
			} else if(m_pFlowerTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerShowStaticInfo;
				interaction.towerType = flowerTower_lvl1;
				m_isShowingTowerInfo = true;
			} else if(m_pSnifitTowerButton->collisionCheck(mousePos)) {
				interaction.type = towerShowStaticInfo;
				interaction.towerType = snifitTower_lvl1;
				m_isShowingTowerInfo = true;
			} else if(m_isShowingTowerInfo) {
				interaction.type = towerHideStaticInfo;
				m_isShowingTowerInfo = false;
			} 

		} else {
			//No interaction
			return(interaction);
		}
		return(interaction);
	}
	
}
